package com.briup.cms.service.impl;

import com.briup.cms.bean.BaseLog;
import com.briup.cms.bean.BaseUser;
import com.briup.cms.constant.CmsConstants;
import com.briup.cms.dao.BaseLogMapper;
import com.briup.cms.dao.BaseUserMapper;
import com.briup.cms.dao.extend.BaseLogExtendMapper;
import com.briup.cms.dao.extend.BaseUserExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IBaseUserService;
import com.briup.cms.utils.JudgeAdmin;
import com.briup.cms.utils.JwtUtil;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @Description
 * @date 2021/7/17 16:17
 */

@Service
public class BaseUserServiceImpl implements IBaseUserService {
    @Autowired
    private BaseUserExtendMapper baseUserExtendMapper;

    @Autowired
    private BaseLogExtendMapper baseLogExtendMapper;

    @Autowired
    private BaseLogMapper baseLogMapper;

    @Autowired
    private BaseUserMapper baseUserMapper;

    @Override
    public BaseUser selectByPrimaryKey(Integer id) {
        return baseUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insert(BaseUser baseUser) {
        baseUserMapper.insert(baseUser);
    }

    @Override
    public void update(BaseUser baseUser) {
        baseUserMapper.updateByPrimaryKey(baseUser);
    }

    @Override
    public void delete(Integer id) {
        baseUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public String login(String username, String password) throws CustomerException {
        BaseUser user = baseUserExtendMapper.findUserByName(username);

        if(user==null){
            throw new CustomerException(MessageStatus.USER_NOT_EXIST);
        }

        if(user.getStatus() != CmsConstants.USER_STATUS_NORMAL){
            throw new CustomerException(MessageStatus.USER_ACCOUNT_FORBIDDEN);
        }

        if(user.getRole() != CmsConstants.SYSTEM_ROLE_ADMIN && user.getRole()!=CmsConstants.SYSTEM_ROLE_USER){
            throw new CustomerException(MessageStatus.USER_NOT_EXIST);
        }

        if(!user.getPassword().equals(password)){
            throw new CustomerException(MessageStatus.USER_LOGIN_ERROR);
        }

        //把role值加入token
        JudgeAdmin a=new JudgeAdmin();
        a.judgeAdmin(user.getRole());




        //日志生成功能 添加用户名和姓名给token值
        HashMap<String, Object> userInfo = new HashMap<>();
        userInfo.put("username",user.getUsername());
        userInfo.put("realname",user.getRealname());
        userInfo.put("role",user.getRole());
//        System.out.println("保存的token："+userInfo);
        return JwtUtil.sign(user.getUserId().toString(),userInfo);






    }

    @Override
    public BaseUser currentUserInfo(String token) throws CustomerException {

        String id = JwtUtil.getUserId(token);
//        System.out.println(id+"id值"+Integer.parseInt(id));
        BaseUser user = baseUserMapper.selectByPrimaryKey(Integer.parseInt(id));
//        System.out.println(user);
        user.setPassword(null);

        return user;
    }

    @Override
    public PageInfo<BaseUser> pageQuery(int pageNum, int pageSize, String username, Integer role) {
        PageHelper.startPage(pageNum,pageSize,true);

        List<BaseUser> list = baseUserExtendMapper.pageQuery(username, role);

        return new PageInfo(list);
    }

//    @Override
//    public BaseUser findById(int id) {
//        return baseUserMapper.selectByPrimaryKey(id);
//    }


    @Transactional
    @Override
    public void changeStatus(int id, int status) throws CustomerException {
        BaseUser userFromDB = baseUserMapper.selectByPrimaryKey(id);
        if (userFromDB == null) {
            throw new CustomerException(MessageStatus.USER_NOT_EXIST);
        }
        BaseUser u = new BaseUser();
        u.setUserId(id);
        u.setStatus(status);
        baseUserMapper.updateByPrimaryKeySelective(u);
    }

    @Transactional
    @Override
    public void deleteById(int id) throws CustomerException {
        BaseUser user = baseUserMapper.selectByPrimaryKey(id);
        if (user == null) {
            throw new CustomerException(MessageStatus.USER_NOT_EXIST);
        }
        if(user.getRole() == CmsConstants.SYSTEM_ROLE_ADMIN) {
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ADNIN_FORBID_DEL);
        }
        baseUserMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public void updateUserIcon(int id, String userIcon) throws CustomerException {
        BaseUser user = baseUserMapper.selectByPrimaryKey(id);
        if (user == null) {
            throw new CustomerException(MessageStatus.USER_NOT_EXIST);
        }
        user.setIcon(userIcon);
        baseUserMapper.updateByPrimaryKey(user);
    }

}